package com.dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 封装对于数据库底层的操作
 * @author lovemanai
 *
 */
public class DBDao {
	private static Connection conn;
	
	private static String URL;
	private static String USERNAME;
	private static String PASSWORD;
	private static String DRIVER;

	/**
	 * 获得数据库配置信息
	 * 包括数据库连接地址，用户名，密码，数据库驱动程序地址
	 */
	public DBDao() {
		Properties p = new Properties();
		try {
			p.load(this.getClass().getClassLoader().getResourceAsStream("conf/dbproperties.properties"));
			URL = p.getProperty("URL");
			PASSWORD = p.getProperty("PASSWORD");
			USERNAME = p.getProperty("USERNAME");
			DRIVER  = p.getProperty("DRIVER");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 通过getConfig()方法获得配置信息获取一个Connection连接
	 * 将连接赋值给成员变量conn
	 */
	private void getConn() {
		try {
			Class.forName(DRIVER);
			conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 执行一条增删改语句
	 * @param sql 执行的sql语句，形如:insert into table value(1,2,3)语句中不含有?占位符
	 */
	public int excuteDML(String sql) {
		if(conn == null) getConn();
		Statement stat = null;
		try {
			stat = conn.createStatement();
			return stat.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(conn,stat);
		}
		return 0;
	}
	
	/**
	 * 执行一条增删改语句
	 * @param sql 执行的sql语句，形如:insert into table value(?,?,?)语句中含有?占位符
	 * @param obj 对象数组，用来替换sql语句中的?占位符
	 */
	public int excuteDML(String sql,  Object... obj) {
		List<String> sbList = new ArrayList<String>();
		String str = null;
		int j = 0;
		StringBuilder sqlRebulit = new StringBuilder();
		for(int i = 0; i < sql.length(); i++) {
			char[] c = new char[1];
			c[0] = sql.charAt(i);
			str = new String(c);
			if(str.equals("?")) {
				if(obj[j].getClass().getTypeName().equals("java.lang.Integer")) {
					str = (Integer)obj[j] + "";
					j++;
				} else if(obj[j].getClass().getTypeName().equals("java.lang.Float")) {
					str = (Float)obj[j] + "";
					j++;
				} else if(obj[j].getClass().getTypeName().equals("java.lang.Double")) {
					str = (Double)obj[j] + "";
					j++;
				}else if(obj[j].getClass().getTypeName().equals("java.lang.String")) {
					str = "'" + (String)obj[j] + "'";
					j++;
				}
			}
			
			sbList.add(str);
		}
		
		for(int i = 0; i < sbList.size(); i++) {
			sqlRebulit.append(sbList.get(i));
		}
		return excuteDML(sqlRebulit.toString());
	}

	/**
	 * 执行一条查询语句
	 * @param sql 查询语句
	 * @return 返回查询获得结果集
	 */
	public ResultSet excuteSelect(String sql) {
		ResultSet rs = null;
		if(conn == null) getConn();
		try {
			Statement stat = conn.createStatement();
			rs = stat.executeQuery(sql);

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	/**
	 * 关闭Connection, Statement, ResultSet
	 * @param obj 连接对象
	 */
	public void close(Object... obj) {
		for(int i = 0; i < obj.length; i++) {
			if(obj[i].getClass().getTypeName().equals("java.sql.Connection")) {
				Connection co = (Connection)obj[i];
				try {
					if(co != null) co.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			} else if(obj[i].getClass().getTypeName().equals("java.sql.ResultSet")) {
				ResultSet rs = (ResultSet)obj[i];
				try {
					if(rs != null) rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			} else if(obj[i].getClass().getTypeName().equals("java.sql.Statement")) {
				Statement stat = (Statement)obj[i];
				try {
					if(stat != null) stat.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
 }
